#include "window.h"

Window::Window(QWidget *parent) : QMainWindow(parent)
{
    setFixedSize(640, 640);

//    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnBottomHint);

    quitAction = new QAction(tr("&Quit"), this);

    QPixmap newpix("new.png");
    QPixmap quitpix("quit.jpg");

    viewaclock = new QAction("&View analogic clock", this);
    viewaclock->setCheckable(true);
    viewaclock->setChecked(true);
    viewaclock->setShortcut(tr("CTRL+A"));

    viewdclock = new QAction("&View digital clock", this);
    viewdclock->setCheckable(true);
    viewdclock->setChecked(true);
    viewdclock->setShortcut(tr("CTRL+D"));

    viewrclock = new QAction("&View radio clock", this);
    viewrclock->setCheckable(true);
    viewrclock->setChecked(true);
    viewrclock->setShortcut(tr("CTRL+R"));

    QMenu *file;
    file = menuBar()->addMenu("&File");
    file->addAction(new QAction(newpix, "&New", this));
    file->addSeparator();
    file->addAction(viewaclock);
    file->addAction(viewdclock);
    file->addAction(viewrclock);
    file->addSeparator();
    quitAction->setShortcut(tr("CTRL+Q"));
    file->addAction(quitAction);

    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(viewaclock, SIGNAL(triggered()), this, SLOT(toggleStatusbar()));
    connect(viewdclock, SIGNAL(triggered()), this, SLOT(toggleStatusbar()));
    connect(viewrclock, SIGNAL(triggered()), this, SLOT(toggleStatusbar()));

    QToolBar *toolbar = addToolBar("main toolbar");
    toolbar->addAction(QIcon(newpix), "New File");
    toolbar->addSeparator();
    QAction *quitb = toolbar->addAction(QIcon(quitpix),
                                        "Quit Application");
    toolbar->addAction(viewaclock);
    connect(quitb, SIGNAL(triggered()), qApp, SLOT(quit()));

    m_bouton = new QPushButton("Elapsed", this);
    m_bouton->setFont(QFont("Comic Sans MS", 14));

    q_bouton = new QPushButton("Quitter", this);
    q_bouton->setFont(QFont("Comic Sans MS", 14));

    connect(m_bouton, SIGNAL(clicked()), this, SLOT(showMessage()));
    connect(q_bouton, SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    this->setObjectName(tr("MAIN"));

    QMainWindow *test = new QMainWindow();

    QDockWidget* dockdigital = new QDockWidget(tr("Digital"), test);
    dclock = new DigitalClock(this);
    dockdigital->setWidget(dclock);
    //    test->addDockWidget(Qt::TopDockWidgetArea, dockdigital);

    QDockWidget* dockanalogic = new QDockWidget(tr("Analogic"), test);
    aclock = new AnalogiClock(this);
    dockanalogic->setWidget(aclock);
    test->addDockWidget(Qt::BottomDockWidgetArea, dockanalogic);
    //    test->tabifyDockWidget(dockdigital,dockanalogic);

    QDockWidget* dockradio = new QDockWidget(tr("Analogic"), test);
    rclock = new RadioClock(this);
    dockradio->setWidget(rclock);
    test->addDockWidget(Qt::BottomDockWidgetArea, dockradio);
    //    test->tabifyDockWidget(dockdigital,dockanalogic);

    QWidget *main = new QWidget();

    QHBoxLayout *vbox1 = new QHBoxLayout;
    vbox1->addWidget(m_bouton);
    vbox1->addWidget(q_bouton);

    QDockWidget* dock = new QDockWidget(tr("Paragraphs"), this);
    QListWidget* paragraphsList = new QListWidget(dock);
    paragraphsList->addItems(QStringList()
                             << "Thank you for your payment which we have received today."
                             << "Your order has been dispatched and should be with you "
                             "within 28 days.");

    dock->setWidget(paragraphsList);
    dock->setFixedWidth(150);
    //    dock->setWindowState(Qt::WindowMinimized);
    //        dock->setTitleBarWidget(c_bouton); does'nt works

    QWidget *mytitle = new QWidget();
    QHBoxLayout *t1 = new QHBoxLayout;
    t1->addWidget(new QPushButton("-", this));
    t1->addWidget(new QPushButton("[]", this));
    t1->addWidget(new QPushButton("X", this));
    QHBoxLayout *t2 = new QHBoxLayout();
    t2->addWidget(new QPushButton("title", this));
    t2->addLayout(t1);
    mytitle->setLayout(t2);

    //        dock->setTitleBarWidget(new QPushButton("plop", this));
    //    dock->setTitleBarWidget(mytitle);
    addDockWidget(Qt::RightDockWidgetArea, dock);

    QDockWidget* dockeclock = new QDockWidget(tr("Elapsed"), this);
    ElapsedClock* eclock = new ElapsedClock(this,5);
    eclock->setMinimumWidth(150);
    dockeclock->setWidget(eclock);
    addDockWidget(Qt::RightDockWidgetArea, dockeclock);

    QDockWidget* dockcdclock = new QDockWidget(tr("Count Down"), this);
    CountdownClock* cdclock = new CountdownClock(this,5);
    cdclock->setMinimumWidth(150);
    dockcdclock->setWidget(cdclock);
    addDockWidget(Qt::RightDockWidgetArea, dockcdclock);

    //    QWidget *dial = new QWidget();
    //    dial->setWindowState(Qt::WindowMinimized);
    //    QHBoxLayout *vbox2 = new QHBoxLayout;
    //    vbox2->addWidget(new QPushButton("test", this));
    //    dial->setLayout(vbox2);

    //    QPalette ppp(palette());
    //    ppp.setColor(QPalette::Background, Qt::green);
    //    dial->setAutoFillBackground(true);
    //    dial->setPalette(ppp);

    //    QDockWidget* dock2 = new QDockWidget(tr("test"), this);
    //    dock2->setWidget(dial);
    //    //    dock2->setWindowFlags(Qt::WindowMinimizeButtonHint);
    //    dock->setTitleBarWidget(new QPushButton("fake", this));
    //    dock2->titleBarWidget()->setVisible(false);
    //    addDockWidget(Qt::BottomDockWidgetArea, dock2);

    dclock = new DigitalClock(this, 8);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(aclock);
    mainLayout->addWidget(dclock);
    mainLayout->addWidget(rclock);
    mainLayout->addLayout(vbox1);

    main->setLayout(mainLayout);

    setCentralWidget(main);

    createTrayIcon();
    trayIcon->show();

    setWindowTitle(tr("My_Qt_Clock"));
    resize(400, 300);
}

void Window::showMessage()
{
    trayIcon->showMessage(tr("Elapsed"), dclock->elapsed_time, QSystemTrayIcon::Information,
                          3000);
}

void Window::toggleStatusbar()
{
    if (viewaclock->isChecked())
    {
        aclock->setVisible(true);
    }
    else
    {
        aclock->setVisible(false);
    }

    if(viewdclock->isChecked())
    {
        dclock->setVisible(true);
    }
    else
    {
        dclock->setVisible(false);
    }

    if(viewrclock->isChecked())
    {
        rclock->setVisible(true);
    }
    else
    {
        rclock->setVisible(false);
    }

}

void Window::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon("./images/trash.svg"));
    trayIcon->setContextMenu(trayIconMenu);
}
